/**
 * Created by Baggio on 2017-11-20.
 */
$(function () {

    $(".list ul li").click(function () {
        var index = $(this).index();
        switch (index) {
            case 0:
                location.href = "/order/orderList.html";
                break;
            case 1:
                $(".message_box5").show();
                $(".message_box4").hide()
                break;
            case 2:
                $(".message_box4").show();
                $(".message_box5").hide();
                break;
            case 3:
                logOut();
                break;
        }


    });
    $(".message_box3 .btn1").click(function () {
        $(".message_box3").hide();
    });
    /**
     * 修改密码
     */
    $(".message_box4 input[type=button]").click(function () {

        var passWord = $.trim($(".message_box4 .agin input[type=password]").val());

        var passWord_2 = $.trim($(".message_box4 .new input[type=password]").val());
        var passWord_used = $.trim($(".message_box4 .used input[type=password]").val());
        if(passWord_used==""){
            alert("原密码不能为空!");
            return;
        }
        if(passWord!=passWord_2){
            alert("两次密码输入不一样!");
            return;
        }
        console.log(passWord);
        var postData = {passWord: passWord,passWordOld:passWord_used};
        modifyUser(postData);
        $(".message_box4").hide();
        $(".message_box4").find(".used").find("input").val("");
        $(".message_box4").find(".new").find("input").val("");
        $(".message_box4").find(".agin").find("input").val("");

    });
    /**
     * 修改地址
     */

    $(".message_box5 .btn").click(function () {
        var userAddress = $(".message_box5 .recipient-name .message_board_5").html();
        if (userAddress == undefined || userAddress == '') {
            showMessage("地址不能为空！");
            return;
        }
        var postData = {userAddress: userAddress};
        modifyUser(postData);
        $(".message_box5").hide();
    });
    $(".contact_4").click(function () {
        $(".message_box1").show();
        $(".login_box").hide();
        $(".register_box1").hide();


    });

    function modifyUser(postData) {
        $.ajax({
            type: "post",
            url: "/user/modifyUser.html",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                if (res.code != 1) {
                    showMessage(res.msg);
                    return;
                }
                showMessage(res.msg);
            }

        });
    }

    /**
     * 打开登录弹窗
     */
    $(".login").click(function () {
        $(".login_box").show();
        $(".register_box1").hide();
        $(".message_box1").hide();

    });
    $(".login_box .forget_link").click(function () {
        $(".login_box").hide();
        $(".register_box2").show();
    });
    $(".login_box .register_link").click(function () {
        $(".login_box").hide();
        $(".register_box1").show();
    })

    $(".register").click(function () {
        $(".register_box1").show();
        $(".login_box").hide();
        $(".message_box1").hide();

    });
/*    $(".verification_2").click(function () {

        var phone = $(".register_box1").find("input[name='phone']").val();
       // var passWord = $.trim($(".message_box4 .agin input[type=text]").val());
        if (!checkMobile(phone)) {
            showMessage("手机号格式错误！");
            return;
        }

        var postData = {phone: phone}
        $.ajax({
            type: "post",
            url: "/user/getSMSCode.html",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                if (res.code != 1) {
                    showMessage(res.msg);
                    return;
                }
                showMessage(res.msg);
            }

        });


    });*/
    function logOut() {
        var postData = {}
        $.ajax({
            type: "post",
            url: "/user/logOut.html",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                console.log(res);
                if (res.code != 1) {
                    showMessage(res.msg);
                    return;
                }
                showMessage(res.msg);
                location.reload();
            }

        });
    }

    /*登录*/
    $(".sub1").click(function () {
        $("#login").ajaxSubmit({
            type: "POST",
            url: '/user/login.html',
            data: $('#login').serialize(),// 你的formid
            async: false,
            error: function (request) {
                alert("Connection error");
            },
            success: function (data) {

                console.log(data);
                var data = JSON.parse(data);
                if (data.code != 1) {
                    showMessage(data.msg);
                    return;
                }

                showMessage(data.msg);
                $(".register_box1").hide();
                location.reload();
            }

        });

    });


    /*注册*/
    $(".sub2").click(function () {

        var phone = $(".register_box1").find("input[name='phone']").val();
        if (!checkMobile(phone)) {
            alert("手机号格式错误！");
            return;
        }
        var userName = $(".register_box1").find("input[name='userName']").val();
        if (isEmpty(userName)) {
            alert("用户名不能为空!");
            return;
        }
        var userAddress = $(".register_box1").find("input[name='userAddress']").val();
        if (isEmpty(userAddress)) {
            alert("用户地址不能为空!");
            return;
        }
        var passWord = $(".register_box1").find("input[name='passWord']").val();
        if (isEmpty(passWord)) {
            alert("密码不能为空!");
            return;
        }
        var passWord2 = $(".register_box1").find("input[name='passWord2']").val();
        if (isEmpty(passWord2)) {
            alert("密码2不能为空!");
            return;
        }
        if (passWord2 != passWord) {
            alert("2次密码输入不一致！");
            return;
        }

        $("#register").ajaxSubmit({
            type: "POST",
            url: '/user/regedit.html',
            data: $('#register').serialize(),// 你的formid
            async: false,
            error: function (request) {
                alert("Connection error");
            },
            success: function (data) {
               var data=JSON.parse(data);
                if (data.code != 1) {
                    showMessage(data.msg);
                    return;
                }
                showMessage("注册成功！");
                $(".register_box1").hide();
                location.reload();
            }
        });

        $(this).parents('.login_box').find('input').val('');
        $(this).parents('.register_box1').hide();

    });

    // 获取验证码
    var counting = false;
    $(".register_box1 .verification_2").click(function () {
        var timer, count = 60,
            that = $(this);
        var $btn = $(this), //获取验证码的按钮
            phone = $(".register_box1 input[name='phone']").val();
            console.log(phone);
            //phone = $('input[name="phone"]').val(), // 手机号码控件的值
            phoneRight = phone.match(/^[1]{1}[\d]{10}$/); // 手机号码是否符合格式
        if (!checkMobile(phone)) {
            showMessage("手机号格式错误！");
            return;
        }


        var postData = {phone: phone}
        $.ajax({
            type: "post",
            url: "/user/getSMSCode.html",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                if (res.code != 1) {
                    showMessage(res.msg);
                    return;
                }
                showMessage("短信已发送！");
            }

        });


        /* 倒计时 */
        /*延迟一秒问题*/
        setTimeout(function () {
            that.val(count + '(s)');
        }, 0)
        /*一分钟后再次发送*/
        timer = setInterval(function () {
            if (count > 1) {
                count--;
                // alert($(this).html())
                that.val(count + '(s)');
            } else {
                counting = false;
                count = 60;
                that.val("获取验证码");
                that.removeClass("waiting");
                clearInterval(timer);
            }
        }, 1000)


    });




    /*留言*/
    $(".sub3").click(function () {

        var message = $(".message_board_1").html();
        if (message == '') {
            alert("内容必须填写");
            return;
        }
        $("#message").val(message);

        var phone = $("#phone").val();
        if (phone == '') {
            alert("请输入电话！");
            return;
        }
        if (!checkMobile(phone)) {
            alert("电话格式不对！");
            return;
        }


        $("#messageBoard").ajaxSubmit({
            type: "POST",
            url: '/messageBoard/addMessageBoard.html',
            data: $('#messageBoard').serialize(),// 你的formid
            async: false,
            error: function (request) {
                alert("Connection error");
            },
            success: function (data) {

                console.log(data);
                var data = JSON.parse(data);
                if (data.code != 1) {
                    alert(data.msg);
                    return;
                }

                alert(data.msg);
                $(".message_box1").hide();
            }
        });
    });


    /*忘记密码  改密*/
    $(".sub4").click(function () {
        var password4 = $(".register_box2").find("input[name='password']").val();
        if (isEmpty(password4)) {
            alert("新密码不能为空!");
            return;
        }
        var password4_1 = $(".register_box2").find("input[name='password2']").val();
        if (isEmpty(password4_1)) {
            alert("密码2不能为空!");
            return;
        }
        var phone4 = $(".register_box2 .phone4").val();
        if (phone4 == "") {
            alert("请输入手机号!");
            return;
        }
        if (!checkMobile(phone4)) {
            alert("电话格式不对！");
            return;
        }

        var code = $(".register_box2 .verification4_1").val();

        var postData = {passWord: password4, code: code, phone: phone4};
        modifyUser(postData);

        $('.tt_one').hide();
        $('.tt_one').find('input').val('');

    });


    // 获取验证码
    var counting = false;
    $(".register_box2 .verification4_2").click(function () {
        var timer, count = 60,
            that = $(this);
        var $btn = $(this), //获取验证码的按钮
            phone = $(".register_box2 .phone4").val(),
            //phone = $('input[name="phone"]').val(), // 手机号码控件的值
            phoneRight = phone.match(/^[1]{1}[\d]{10}$/); // 手机号码是否符合格式
        if (!checkMobile(phone)) {
            showMessage("手机号格式错误！");
            return;
        }


        var postData = {phone: phone}
        $.ajax({
            type: "post",
            url: "/user/getSMSCode.html",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                if (res.code != 1) {
                    showMessage(res.msg);
                    return;
                }
                showMessage(res.msg);
            }

        });


                /* 倒计时 */
                /*延迟一秒问题*/
                setTimeout(function () {
                    that.val(count + '(s)');
                }, 0)
                /*一分钟后再次发送*/
                timer = setInterval(function () {
                    if (count > 1) {
                        count--;
                        // alert($(this).html())
                        that.val(count + '(s)');
                    } else {
                        counting = false;
                        count = 60;
                        that.val("获取验证码");
                        that.removeClass("waiting");
                        clearInterval(timer);
                    }
                }, 1000)


    });


    $(".esc .esc_e").click(function () {
        $(this).parent().parent().hide();
    });


    /*$(".information .state .drop").hover(function(){
     $(".information .state .list").show();
     });*/


    $(".information .state .nickname").click(function () {
        $(".information .state .list").toggle();
    });
    $(".information .state .list").mouseover(function () {
        $(this).css({"display": "block"});
    });
    $(".information .state .list").mouseout(function () {
        $(this).css({"display": "none"});
    });


    //switchPwd()
});
/*
 function switchPwd() {
 var passwordeye = $('#passwordeye');
 var showPwd = $("#password");
 passwordeye.off('click').on('click', function () {
 if (passwordeye.hasClass('invisible')) {
 passwordeye.removeClass('invisible').addClass('visible');//密码可见
 showPwd.prop('type', 'text');
 } else {
 passwordeye.removeClass('visible').addClass('invisible');//密码不可见
 showPwd.prop('type', 'password');
 }
 ;
 });
 }
 */

function showMessage(message) {
    $(".message_box3 .code h3").html("");
    $(".message_box3 .code h3").html(message);
    $(".message_box3").show();


}
